﻿using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Mall.Core;
using System.Linq;
using Solution;

namespace Mall.AdminSystem.Controllers
{
    public class PassportController : Controller
    {
        private AdminCore aCore = new AdminCore();
        //
        // GET: /Passport/

        public ActionResult Login()
        {
            //ViewBag.LoginTips = Md5Extensions.MD5Encrypt("123456888");
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection obj)
        {
            string tips = "";
            string uname = obj["uname"].Trim();
            var user = aCore.LoadEntity(o => o.A_Account == uname);
            if (user != null)
            {
                string pwdCode = user.A_EncryptCode;
                if (user.A_Pwd.Equals(CryptoService.Md5Encrypt(obj["pwd"] + pwdCode)))
                {
                    string roles = string.Join(",", user.Sys_AdminRole.Select(p => { return p.AR_RoleId; }));
                    //存储票据
                    Response.SetAuthCookie(user.A_Id.ToString(), roles, true);

                    SessionHelper.SetSession("loginUser", user);

                    return RedirectToAction(actionName: "index", controllerName: "console");
                }
                else
                {
                    tips = "帐号密码错误";
                }
            }
            else
            {
                tips = "帐号不存在";
            }
            ViewBag.LoginTips = tips;
            return View();
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {

            SessionHelper.ClearSession("GLOBAL_ROLE_MENU" + FormTicketHelper.GetUserId());
            SessionHelper.ClearSession("loginUser");
            FormTicketHelper.Logout();
            //CookieHelper.ClearCookie("manager");
            return RedirectToAction("Login");
        }

        public ActionResult Test()
        {
            NoticeCore nCore = new NoticeCore();
            return Content(nCore.LoadEntity().N_Title);
        }
    }
}
